package com.service.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.service.common.config.RuoYiConfig;
import com.service.common.core.domain.entity.StudentInfo;
import com.service.common.core.domain.model.LoginUser;
import com.service.common.utils.file.FileUploadUtils;
import com.service.common.utils.file.MimeTypeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.service.common.annotation.Log;
import com.service.common.core.controller.BaseController;
import com.service.common.core.domain.AjaxResult;
import com.service.common.enums.BusinessType;
import com.service.system.service.IStudentInfoService;
import com.service.common.utils.poi.ExcelUtil;
import com.service.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生信息Controller
 *
 * @author service
 * @date 2024-05-01
 */
@RestController
@RequestMapping("/system/studentInfo")
public class StudentInfoController extends BaseController
{
    @Autowired
    private IStudentInfoService studentInfoService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentInfo studentInfo)
    {
        startPage();
        List<StudentInfo> list = studentInfoService.selectStudentInfoList(studentInfo);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentInfo studentInfo)
    {
        List<StudentInfo> list = studentInfoService.selectStudentInfoList(studentInfo);
        ExcelUtil<StudentInfo> util = new ExcelUtil<StudentInfo>(StudentInfo.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:query')")
    @GetMapping(value = "/{stuId}")
    public AjaxResult getInfo(@PathVariable("stuId") Long stuId)
    {
        return success(studentInfoService.selectStudentInfoByStuId(stuId));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentInfo studentInfo)
    {
        return toAjax(studentInfoService.insertStudentInfo(studentInfo));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentInfo studentInfo)
    {
        return toAjax(studentInfoService.updateStudentInfo(studentInfo));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stuIds}")
    public AjaxResult remove(@PathVariable Long[] stuIds)
    {
        return toAjax(studentInfoService.deleteStudentInfoByStuIds(stuIds));
    }
    /**
     * 上传照片
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:image')")
    @Log(title = "上传照片", businessType = BusinessType.UPDATE)
    @PostMapping("/image")
    public AjaxResult uploadImage(@RequestParam("imageFile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
//            LoginUser loginUser = getLoginUser();
            String imgUrl = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", imgUrl);
                return ajax;
            }
        }
        return error("上传照片异常，请联系管理员");
    }
}
